package top.xzxsrq.pyncm;

import top.xzxsrq.ffmpeg.FFmpegUtils;
import top.xzxsrq.fileviewutils.download.WangYiYunDownload;
import top.xzxsrq.web.download.DownloadManager;
import top.xzxsrq.web.download.entity.PageInfo;

import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PyncmUtils {

    public static String pyncm;
    public static String load;
    public static String[] getPyncmCmd(String ...args) {
        String[] BASE_CMD_FFMPEG = new String[]{
                pyncm,
                "--http",
                "--load",
                load,
                "--lyric-no",
                "none",
                "-m",
                String.valueOf(FFmpegUtils.THREADS_NUMBER)
        };
        String[] cmds = new String[args.length + BASE_CMD_FFMPEG.length];
        System.arraycopy(BASE_CMD_FFMPEG, 0, cmds, 0, BASE_CMD_FFMPEG.length);
        System.arraycopy(args, 0, cmds, BASE_CMD_FFMPEG.length, args.length);
        return cmds;
    }

    public static float downloadPer(String line) {
        // 0.00B: 100.0%|██████████| 18.00/18 00:00<00:00
        String pattern = "\\: (.*?)\\%\\|";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(line);
        if(m.matches()) {
            String group = m.group(1);
            return Float.parseFloat(group);
        }
        return 0f;
    }

    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
        PyncmUtils.pyncm = "E:\\wangYiYunXiaZai\\pyncm.exe";
        PyncmUtils.load = "E:\\wangYiYunXiaZai\\pyncmInfoSave.save";
        String[] cmd = PyncmUtils.getPyncmCmd("--no-overwrite",
                "--output",
                "E:/myService/windows-amd64-filebrowser/fileBrowserData/音乐",
                "--quality",
                "standard",
                "\"playlist=10055287548\"");
        // [E:\wangYiYunXiaZai\pyncm.exe, --http, --load, E:\wangYiYunXiaZai\pyncmInfoSave.save, --lyric-no, none, -m, 4, --no-overwrite, --output, D:/CODE/project/self/FileViewUtils/fileBrowserData/音乐, --quality, standard, "playlist=10055287548"]
        // [E:\wangYiYunXiaZai\pyncm.exe, --http, --load, E:\wangYiYunXiaZai\pyncmInfoSave.save, --lyric-no, none, -m, 4, --no-overwrite, --output, E:/myService/windows-amd64-filebrowser/fileBrowserData/音乐, --quality, standard, "playlist=10055287548"]
        WangYiYunDownload.WangYiYunDownloadDT wangYiYunDownloadDT = new WangYiYunDownload.WangYiYunDownloadDT();
        wangYiYunDownloadDT.setDownloadId("454654");
        WangYiYunDownload wangYiYunDownload = new WangYiYunDownload();
        wangYiYunDownloadDT.setWangYiYunDownload(wangYiYunDownload);
        wangYiYunDownloadDT.setCmds(cmd);
        wangYiYunDownloadDT.setSavePath("E:/myService/windows-amd64-filebrowser/fileBrowserData/音乐");
        wangYiYunDownloadDT.setPageInfo(new PageInfo());
        wangYiYunDownloadDT.setDownloadType(wangYiYunDownload.getDownloadType());
        Future<Void> submit = DownloadManager.submit(wangYiYunDownloadDT);
        // 阻塞主线程
        submit.get();
        DownloadManager.threadPoolExecutor.shutdown();
    }
}
